home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / commands / eq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-27  |  3.9 KB  |  184 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     eq.c
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. /* ======================================== */
  16.  
  17. // possible choices: DO_EQ, DO_NE, DO_GT, DO_LT, DO_GE, DO_LE
  18.  
  19. #define DO_LE
  20.  
  21. /* ======================================== */
  22.  
  23. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  24. #include <A4Stuff.h>
  25. #endif
  26.  
  27. #include "nshc.h"
  28.  
  29. #include "arg_utl.proto.h"
  30. #include "nshc_utl.proto.h"
  31. #include "str_utl.proto.h"
  32.  
  33. /* ======================================== */
  34.  
  35. void eq_name( t_nshc_calls *nshc_calls );
  36.  
  37. void eq_name( t_nshc_calls *nshc_calls )
  38. {
  39. #if defined DO_EQ
  40.     nshc_calls->NSH_putStr_err( "\p.eq." );
  41. #elif defined DO_NE
  42.     nshc_calls->NSH_putStr_err( "\p.ne." );
  43. #elif defined DO_GT
  44.     nshc_calls->NSH_putStr_err( "\p.gt." );
  45. #elif defined DO_LT
  46.     nshc_calls->NSH_putStr_err( "\p.lt." );
  47. #elif defined DO_LE
  48.     nshc_calls->NSH_putStr_err( "\p.le." );
  49. #elif defined DO_GE
  50.     nshc_calls->NSH_putStr_err( "\p.ge." );
  51. #endif
  52. }
  53.             
  54. /* ========================================== */
  55.  
  56. int    eq_str_less( char *p, char *q );
  57.  
  58. int    eq_str_less( char *p, char *q )
  59. {
  60.     char jim,bob;
  61.     
  62.     do {
  63.         jim = *p++;
  64.         bob = *q++;
  65.         if ( jim != bob )
  66.             return( jim < bob );
  67.     } while ( jim );
  68.             
  69.     return(0);
  70. }
  71.  
  72. /* ========================================== */
  73.  
  74. int    eq_str_more( char *p, char *q );
  75.  
  76. int    eq_str_more( char *p, char *q )
  77. {
  78.     char jim,bob;
  79.     
  80.     do {
  81.         jim = *p++;
  82.         bob = *q++;
  83.         if ( jim != bob )
  84.             return( jim > bob );
  85.     } while ( jim );
  86.             
  87.     return(0);
  88. }
  89. /* ======================================== */
  90.  
  91. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  92. {
  93.     int        numeric;
  94.     char    *str1;
  95.     char    *str2;
  96.     long    value1;
  97.     long    value2;
  98.     int        result;
  99.     
  100. #ifdef __MWERKS__
  101.     long oldA4  = SetCurrentA4();
  102. #endif
  103.     
  104.     result = -1;
  105.     
  106.     // exit if the version of our nshc.h does not match the one the application used.
  107.  
  108.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) goto Exit;
  109.     
  110.     if ( nshc_parms->argc != 3 ) {
  111.         nshc_calls->NSH_putStr_err( "\pUsage: " );
  112.         eq_name( nshc_calls );
  113.         nshc_calls->NSH_putStr_err( "\p Value1 Value2\r" );
  114.         nshc_parms->result = NSHC_ERR_PARMS;
  115.         nshc_parms->action = nsh_idle;
  116.         goto Exit;
  117.         }
  118.     
  119.     numeric = nshc_is_numeric_operand(nshc_parms,1) &&
  120.               nshc_is_numeric_operand(nshc_parms,2);
  121.  
  122.     if (numeric) {
  123.         value1 =  arg_to_num(nshc_parms,1);
  124.         value2 =  arg_to_num(nshc_parms,2);
  125.         #if defined DO_EQ
  126.             result = !( value1 == value2 );
  127.         #elif defined DO_NE
  128.             result = !( value1 != value2 );
  129.         #elif defined DO_GT
  130.             result = !( value1 > value2 );
  131.         #elif defined DO_LT
  132.             result = !( value1 < value2 );
  133.         #elif defined DO_LE
  134.             result = !( value1 <= value2 );
  135.         #elif defined DO_GE
  136.             result = !( value1 >= value2 );
  137.         #endif
  138.         }
  139.     else {
  140.         str1 = &nshc_parms->arg_buf[ nshc_parms->argv[1] ];
  141.         str2 = &nshc_parms->arg_buf[ nshc_parms->argv[2] ];
  142.         #if defined DO_EQ
  143.             result = !( cStrEqual( str1, str2 ) );
  144.         #elif defined DO_NE
  145.             result =  ( cStrEqual( str1, str2 ) );
  146.         #elif defined DO_GT
  147.             result = !( eq_str_more( str1, str2 ) );
  148.         #elif defined DO_LT
  149.             result = !( eq_str_less( str1, str2 ) );
  150.         #elif defined DO_LE
  151.             if ( cStrEqual( str1, str2 ) )
  152.                 result = 0;
  153.             else
  154.                 if ( eq_str_less( str1, str2 ) )
  155.                     result = 0;
  156.                 else
  157.                     result = 1;
  158.         #elif defined DO_GE
  159.             if ( cStrEqual( str1, str2 ) )
  160.                 result = 0;
  161.             else
  162.                 if ( eq_str_more( str1, str2 ) )
  163.                     result = 0;
  164.                 else
  165.                     result = 1;
  166.         #endif
  167.         }
  168.         
  169.       nshc_parms->result = result;
  170.         
  171.     // tell the application we are done - for any requested action
  172.  
  173.     nshc_parms->action = nsh_idle;
  174.  
  175. Exit:
  176.  
  177. #ifdef __MWERKS__
  178.     SetA4(oldA4);        // CodeWarrior needs to restore A4
  179. #else
  180.     ;                    // Think needs a ; to go with the Exit label
  181. #endif
  182. }
  183.  
  184.